package com.cyf.db.mapper.smartCity;

import com.cyf.db.bean.FeeDetail;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

@Mapper
public interface LocalFeeDetailMapper {

	@Select("select * from local_feedetail where inoutParkingId = #{id} and parkingId = #{parkingId}")
    List<FeeDetail> getByInoutParkingId(@Param("id") Integer id, @Param("parkingId") Integer parkingId);

    @Select("select wechatFee,actualFee,localFeedetailId, parkingId, id,paidOnline from local_feedetail where inoutParkingId = #{inoutParkingId} and parkingId = #{parkingId}")
    List<FeeDetail> listFeeByInoutParkingId(@Param("inoutParkingId") Integer inoutParkingId, @Param("parkingId") Integer parkingId);

	// 获取某收费员现金收费金额，免费金额
    @Select("select sum(actualfee) actualfee, sum(discountfee + freefee + couponfee) couponfee " +
            "from local_feedetail " +
            "where parkingId = #{parkingId} and createdUserId = #{createdUserId} and cashPay = 1 " +
            "and (settleid is null or settleid = 0)")
    FeeDetail getCashFee(@Param("createdUserId") Integer createdUserId, @Param("parkingId") Integer parkingId);

    @Select("select *, localFeedetailId id from local_feedetail where createdUserId = #{personid}  and settleid is null")
    List<FeeDetail> getUnsettleDetail(@Param("personid") int personid);

    @Select("select count(localFeedetailId) from local_feedetail where createdUserId = #{personid}  and settleid is null and parkingId = #{parkingId}")
    Integer getUnsettleCount(@Param("personid") int personid, @Param("parkingId") int parkingId);

    @Select("select settletime from local_feedetail where createdUserId = #{personid} and settleid = #{maxsettleid} and parkingId = #{parkingId} " +
            "order by localFeedetailId desc limit 0, 1")
    Date getLastSettleDetail(@Param("personid") int personid,
                             @Param("maxsettleid") int maxsettleid, @Param("parkingId") int parkingId);

    @Update("update local_feedetail set settleid = #{settleid}, settleTime = #{settleTime} "
            + "where createdUserId = #{personid} and settleid = #{settleid}  and parkingId = #{parkingId} ")
    int updateSettleFeedetail(@Param("settleid") int settleid,
                              @Param("settleTime") String settleTime,
                              @Param("personid") int personid, @Param("parkingId") int parkingId);

    @Update("update local_feedetail set settleid = #{settleid}, settleTime = #{settleTime} "
            + "where createdUserId = #{personid} and settleid is null  and parkingId = #{parkingId} ")
    int updateUnSettleFeedetail(@Param("settleid") int settleid,
                                @Param("settleTime") String settleTime,
                                @Param("personid") int personid, @Param("parkingId") int parkingId);

    @Update("update local_feedetail set settleid = #{settleid}, settleTime = #{settleTime} "
            + "where localFeedetailId = #{id}")
    void updateSettleById(@Param("settleid") int settleid, @Param("settleTime") String settleTime, @Param("id") Integer id);

    @Select("select SUM(actualfee) actualfee from local_feedetail where settleid = #{settleId} and parkingId = #{parkingId}")
    Integer getAcutalFeeBySettleid(@Param("settleId") Integer settleId, @Param("parkingId") Integer parkingId);

    //计算总量
    //获取现金支付订单数量
    @Select("select count(*) from local_feedetail where parkingId = ${parkingId}")
    int getCashOrderTotalNumber(@Param("parkingId") int parkingId);

    //获取现金支付总额
    @Select("select SUM(shouldfee) shouldfee from local_feedetail where parkingId = ${parkingId} and cashPay = 1")
    int getCashIncomeTotalNumber(@Param("parkingId") int parkingId);

    //获取网络支付订单总量
    @Select("select count(*) from payorder where parkingId = ${parkingId}")
    int getOnlineOrderTotalNumber(@Param("parkingId") int parkingId);

    //获取网络支付总额
    @Select("select SUM(shouldFee) shouldFee from payorder where parkingId = ${parkingId} and payWay <> 0")
    int getOnlineIncomeTotalNumber(@Param("parkingId") int parkingId);

    @Select("select count(id) allCount, sum(actualfee) actualfee from local_feedetail where parkingId = ${parkingId} and createdTime >= #{start} and createdTime < #{end} and feeType = '收费'")
    FeeDetail countByParkingIdAndTime(@Param("parkingId") Integer parkingId, @Param("start") String start, @Param("end") String end);

    @Select("SELECT SUM(actualfee) + SUM(wechatFee) " +
            "from local_feedetail " +
            "where parkingId = #{parkingId}  and inoutParkingId = #{inoutParkingId}")
    Integer getSumActualFeeAndWechatFee(@Param("parkingId") Integer parkingId,
                                        @Param("inoutParkingId") Integer localInoutparkingId);

}
